package com.hgy.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args){
        DoubleHeroNode hero1 = new DoubleHeroNode(1, "宋江", "及时雨");
        DoubleHeroNode hero2 = new DoubleHeroNode(2, "卢俊义", "玉麒麟");
        DoubleHeroNode hero3 = new DoubleHeroNode(3, "吴用", "智多星");
        DoubleHeroNode hero4 = new DoubleHeroNode(4, "林冲", "豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();

//        doubleLinkedList.add(hero1);
//        doubleLinkedList.add(hero2);
//        doubleLinkedList.add(hero3);
//        doubleLinkedList.add(hero4);

        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero4);
        doubleLinkedList.addByOrder(hero3);

        doubleLinkedList.list();

        DoubleHeroNode newHeroNode = new DoubleHeroNode(4, "公孙胜", "入云龙");
        doubleLinkedList.update(newHeroNode);
        System.out.println("更新后的链表~~");
        doubleLinkedList.list();

        doubleLinkedList.del(1);
        doubleLinkedList.del(2);
        System.out.println("删除后的链表~~");
        doubleLinkedList.list();
    }
}

class DoubleLinkedList{
    private DoubleHeroNode head = new DoubleHeroNode();

    public DoubleHeroNode getHead() {
        return head;
    }

    //删
    public void del(int no){
        DoubleHeroNode temp = head.getNext();
        while (temp!=null && temp.getNo()!=no){
            temp = temp.getNext();
        }
        if (temp == null){
            System.out.printf("没有编号为%d的节点\n", no);
        }else {
            temp.getPre().setNext(temp.getNext());
            if(temp.getNext()!=null){
                temp.getNext().setPre(temp.getPre());
            }
        }
    }

    //改
    public void update(DoubleHeroNode doubleHeroNode){
        DoubleHeroNode temp = head.getNext();
        while(temp!=null && temp.getNo()!= doubleHeroNode.getNo()){
            temp = temp.getNext();
        }
        if (temp == null) {
            System.out.printf("没有编号为%d的节点\n", doubleHeroNode.getNo());
        }else {
            temp.setName(doubleHeroNode.getName());
            temp.setNickname(doubleHeroNode.getNickname());
        }
    }

    //按序号顺序增加
    public void addByOrder(DoubleHeroNode doubleHeroNode){
        DoubleHeroNode temp = head;
        while(temp.getNext()!=null && temp.getNext().getNo() <= doubleHeroNode.getNo()){
            if (temp.getNext().getNo() == doubleHeroNode.getNo()){
                System.out.printf("编号为%d的节点已经存在\n", doubleHeroNode.getNo());
                return;
            }
            temp = temp.getNext();
        }
        if (temp.getNext() == null){
            temp.setNext(doubleHeroNode);
            doubleHeroNode.setPre(temp);
        }else {
            doubleHeroNode.setNext(temp.getNext());
            temp.setNext(doubleHeroNode);
            doubleHeroNode.setPre(temp);
            doubleHeroNode.getNext().setPre(doubleHeroNode);
        }
    }

    //增
    public void  add(DoubleHeroNode doubleHeroNode){
        DoubleHeroNode temp = head;
        while(temp.getNext()!=null){
            temp = temp.getNext();
        }
        temp.setNext(doubleHeroNode);
        doubleHeroNode.setPre(temp);
    }

    //显示双向链表
    public void list(){
        //判断链表是否为空
        if (head.getNext() == null){
            System.out.println("链表为空");
            return;
        }
        DoubleHeroNode temp = head.getNext();
        while(temp!=null){
            System.out.println(temp);
            temp = temp.getNext();
        }
    }
}

class DoubleHeroNode{
    private int no;
    private String name;
    private String nickname;
    private DoubleHeroNode next;
    private DoubleHeroNode pre;

    public DoubleHeroNode(){}

    public DoubleHeroNode(int no, String name, String nickname){
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "DoubleHeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public DoubleHeroNode getNext() {
        return next;
    }

    public void setNext(DoubleHeroNode next) {
        this.next = next;
    }

    public DoubleHeroNode getPre() {
        return pre;
    }

    public void setPre(DoubleHeroNode pre) {
        this.pre = pre;
    }
}
